// pages/real_name_auth/real_name_auth.ts
export { };
const tc_cos = require('../../utils/tc_cos');
const user = require("../../utils/user")

Page({
    /**
     * 页面的初始数据
     */
    data: {
        front_img: "/static/id_card_front.png",
        back_img: "/static/id_card_back.png",
        app_user_info: { "uuid": "", isValidate: 0 },
        help_info: "点击图片 上传身份证 正反面",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // get app_user_info
        const app_user_info = wx.getStorageSync("app_user_info");
        if (app_user_info === "") {
            user.flush_user_info(this, (_res: any) => { });
        } else {
            this.setData({
                "app_user_info": app_user_info
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    choose_front_image: function (_e: any) {
        this.choose_id_card_image("front");
    },

    choose_back_image: function (_e: any) {
        this.choose_id_card_image("back");
    },

    choose_id_card_image: function (img_type: string) {
        var that = this;
        // check uuid
        const user_uuid = this.data.app_user_info.uuid;
        if (user_uuid === undefined) {
            wx.showToast({
                title: '无效的用户uuid',
                icon: 'error',
                duration: 3000,
                mask: true,
            });
            user.flush_user_info(this, (_res: any) => { });
            return;
        };
        // choose img and upload
        const file_name = "images/" + user_uuid + "_id_card_" + img_type + ".jpg";
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                var tempFilePaths = res.tempFilePaths;
                if (img_type === "front") {
                    that.setData({
                        front_img: tempFilePaths[0],
                    });
                } else {
                    that.setData({
                        back_img: tempFilePaths[0],
                    });
                }
                // post check id card img call back
                let call_back = (err: any, _data: any) => {
                    if (err != null) {
                        wx.showToast(
                            {
                                title: '上传图片失败',
                                icon: 'error',
                                duration: 2000,
                                mask: true
                            }
                        )
                        console.log("upload img", err);
                    } else {
                        that.post_check_img(img_type);
                    }
                };
                // upload img to s3 direct
                tc_cos.postObject(file_name, tempFilePaths[0], call_back);
            }
        });
    },
    post_check_img(_img_type: string) {
        let that = this;
        // 
        let call_back = (res: any) => {
            user.flush_user_info(that, (_res: any) => {
                wx.showToast({
                    title: res.data.info,
                    icon: res.data.code == 0 ? 'error' : 'success',
                    duration: 3000,
                    mask: true,
                });
                console.log('request real_name_auth success');
                console.log(res);
                that.setData({
                    help_info: res.data.info,
                })
                if (that.data.app_user_info.isValidate != 0) {
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 2000);
                }
            });
        }
        user.real_name_auth(this, call_back);

    }
})